// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()

const db = cloud.database();
// 云函数入口函数
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()
  try {
    var block = await db.collection('block').where({
      user_id: wxContext.OPENID,
    }).get();
    if (block.data[0].block == true) {
      return {
        count: 0,
      }
    } else {
      var timeStamp = new Date().getTime + 8 * 60 * 60 * 1000;
      const total = await db.collection('message').where({
        user_id: wxContext.OPENID,
        read: false,
      }).count();
      return {
        count: total.total,
      }
    }
  } catch (e) {
    console.error(e);
  }
}